<?php
namespace jy_jinxiaofee;
defined('BY_JYA') or exit('error');
/*
	分佣佣金
	
*/
class commission extends \plugin{
	function addorder($order){
		$is_open=intval($this->setting['is_open']);
		if(!$is_open){
			return;
		}
		$member=pdo_get('core_members',array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),array('fromuser'));
		$this->commission_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$order['mid'],
				'agent_id'=>$member['fromuser'],
				'ordersn'=>$order['ordersn'],
				'price'=>$order['price'],
				'plugin_name'=>$order['plugin_name']?:'',
				'commission_price'=>$order['price'],
				'commission_money'=>0,
				'ispay'=>0,
				'status'=>0,
				'createtime'=>TIMESTAMP,
		);
		if($member['fromuser']){
			$agent=pdo_get('jy_jinxiaofee_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['fromuser']));
			if($agent['is_agent']){
				//生成分佣订单
				$indata=$this->commission_order;
				$this->insert('order',$indata);
			}
			return true;
		}
		return true;
	}
	/*
		生成佣金
	*/
	function create($order){
		$is_open=intval($this->setting['is_open']);
		if(!$is_open){
			return;
		}
		$this->order=$order;
		$member=pdo_get('core_members',array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),array('fromuser'));
		$myagent=pdo_get('jy_jinxiaofee_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
		if($myagent['is_agent']){
			$this->model('member')->update_agent_level($order['mid'],$order['id']);
		}
		$this->commission_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$this->order['mid'],
				'agent_id'=>$member['fromuser'],
				'ordersn'=>$this->order['ordersn'],
				'price'=>$this->order['price'],
				'plugin_name'=>$this->order['plugin_name']?:'',
				'commission_price'=>$this->order['price'],
				'commission_money'=>0,
				'ispay'=>1,
				'status'=>1,
				'createtime'=>TIMESTAMP,
		);
		if($member['fromuser']){
			$agent=pdo_get('jy_jinxiaofee_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['fromuser']));
			if($agent['is_agent']){
				if($agent['level_id']){
					$level=pdo_get('jy_jinxiaofee_level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				}
				$this->create_commission($agent,1,$level);
				$orderid=pdo_getcolumn('jy_jinxiaofee_order',array('mid'=>$this->order['mid'],'ordersn'=>$this->order['ordersn'],'uniacid'=>$_SESSION['uniacid']),'id');
				if(!$orderid){
					//生成分佣订单
					$indata=$this->commission_order;
					$this->insert('order',$indata);
				}else{
					pdo_update('jy_jinxiaofee_order',array('status'=>1,'ispay'=>1,'commission_money'=>$this->commission_order['commission_money']),array('mid'=>$this->order['mid'],'ordersn'=>$this->order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
			return true;
		}
	}
	function create_commission($member,$commission_level=1,$level=array()){
		if($member && $member['is_agent'] && $member['stop']==0){
			//相应会员存在且是分销商且状态正常			
			if(!$member['level_id']){
				//获取默认等级佣金比例
				$level=$this->plugin_setting('default_level');
				$level['level']=0;
				$level['id']=0;
				$level_id=0;
			}else{
				$level_id=$level['id'];
			}
			$money=0;
			$order_price=$this->commission_order['commission_price'];
			$red_rate=$level['red_rate'];
			$remark=array(
				'level_id'=>$level_id,
				'commission_level'=>$commission_level,
				'order_price'=>$order_price,
			);
			if(substr($red_rate,-1)=='%'){
				$money=intval($order_price*substr($red_rate,0,-1))/100;
			}else{
				$money=$red_rate;
			}
			if($money>0){
				$this->commission_order['commission_money']+=$money;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'level'=>$commission_level,
						'ordersn'=>$this->order['ordersn'],
						'money'=>$money,
						'type'=>1,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>$this->setting['isbuy_jiesuan']?1:0,
				);
				$this->insert('commission',$new_commission);
			}
		}
		return 0;
	}
	public function order_complete($order){
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('ordersn'=>$order['ordersn'],'status'=>0,'uniacid'=>$_SESSION['uniacid']));
		$this->update('order',array('status'=>3),array('ordersn'=>$order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
		return 0;
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if($post['pay_type']=='downline'){
			if(!$post['wechat']){
				exi('请先填写微信号！','error');
			}
			if(!$post['mobile']){
				exi('请先填写联系电话！','error');
			}
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
		}
		//获取可结算佣金
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1);
		if($this->setting['balance_day']){
			//结算天数
			$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
			$params['complete_time <']=$time;
		}
		$money=pdo_getsum('jy_commission_commission',$params,'money');
		if($money<=0){
			exi('没有可结算的佣金！','error');
		}
		$commission_num=$this->count('commission',$params,'id');
		//结算最低佣金检测
		if($money<$this->setting['balance_money']){
			exi('可结算佣金太少，无法结算！','error');
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'wechat'=>$post['wechat'],
			'mobile'=>$post['mobile'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per'] && ($post['pay_type']!='credit' || !$this->setting['balance_credit'])){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		$update=array(
			'status'=>2,
			'balancesn'=>$new_balance['balancesn'],
		);
		$this->update('commission',$update,$params);
		if($post['pay_type']=='credit'){
			$typetitle='余额';
		}
		if($post['pay_type']=='wechat'){
			$typetitle='微信';
		}
		$this->model('notice')->notice_fenxiao_tixian($mid,$money,$typetitle);
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
		return true;
		/*$update=array(
			'status'=>1,
			'balancesn'=>'',
		);
		$balance=$this->get('balance',array('id'=>$balance_id),array('mid','balancesn'));
		if($balance['balancesn']){
			$this->update('commission',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$balance['mid'],'balancesn'=>$balance['balancesn']));
		}*/
		return true;
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no=''){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
		$update=array(
			'status'=>3,
			'pay_trade_no'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		if($pay_type){
			$update['pay_type']=$pay_type;
		}
		$this->update('balance',$update,array('id'=>$balance['id']));
		$this->model('notice')->notice_fenxiao_dakuan($balance['mid'],$balance['final_money']);
		return true;
	}
	/*
		线下打款
	*/
	protected function pay_balance_by_downline(){
		return 'pay_trade_no';
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,'jy_commission','分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		if(!$res['payment_no'] && !$res['batch_id']){
			exi('打款到微信零钱失败！','error');
		}
		return $res['payment_no']?:$res['batch_id'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}
}